$(function page(){
    /**
     *  发送ajax
     */

    $.ajax({
        url: "findAllStu",
        data:{"page":1,"limit":3},
        dataType: "json",
        type:"post",
        success: function (data){
            if(data.code!=0){
                alert("没有信息")
                return;
            }

            build_table(data);
        },
        error:function (XMLHttpRequest, textStatus, errorThrown) {
            alert("请求失败！");
        }
    });

});

function page(data) {
    console.log(data.id);
    var limit = $(data).attr('limit');
    $.ajax({
        url: "findAllStu",
        data:{"page":data.id,"limit":data.limit},
        dataType: "json",
        type:"post",
        success: function (data){
            if(data.code!=0){
                alert("没有信息")
                return;
            }

            build_table(data);
        },
        error:function (XMLHttpRequest, textStatus, errorThrown) {
            alert("请求失败！");
        }
    });
}


function page1(data) {
    var limit = $(data).attr('limit');
    $.ajax({
        url: "findAllStu",
        data:{"page":data.id,"limit":limit},
        dataType: "json",
        type:"post",
        success: function (data){
            if(data.code!=0){
                alert("没有信息")
                return;
            }

            build_table(data);
        },
        error:function (XMLHttpRequest, textStatus, errorThrown) {
            alert("请求失败！");
        }
    });
}



function build_table(result){
    var limit = $("#pageSize").val();

    $("#my_table tbody").empty();

    var list=result.data.list;
    var size = result.data.pages;


    $.each(list,function(index,item){
        var stuindex =  $("<td style='color: #CD950C;'></td>").append(index+1);
        var sname=$("<td style='color: #CD950C;'></td>").append(item.sname);
        var cname=$("<td style='color: #CD950C;'></td>").append(item.cname);
        var tel=$("<td style='color: #CD950C;'></td>").append(item.tel);
        var hour=$("<td></td>").append(item.hour+"节");
        var tname=$("<td></td>").append(item.tname);
        var btn_edit=$("<button  onclick='update(this)' ></button>").addClass("btn btn-outline-brown noExl")//给修改按钮添加 Class标识,
           .append(" 修改");
        btn_edit.attr("id",item.sid);
        btn_edit.attr("data-toggle","modal");
        btn_edit.attr("data-target","#myModal");

        var btn_edit1=$("<button></button>").addClass("btn btn-outline-cyan noExl")//给修改按钮添加 Class标识,
            .append(" 删除");

        btn_edit1.attr("edit-id",item.sid);

        var btn=$("<td></td>").append(btn_edit,btn_edit1);

        $("<tr></tr>").append(stuindex).append(sname).append(cname).append(tel).append(hour).append(tname).append(btn)
            .appendTo("tbody");
    });

    $(".pagination").empty();
    for (let i = 1; i <= size; i++) {
        var li =  $("<li onclick='page1(this)' class='page-item'></li>").append("<a class='page-link'>"+i+"</a>");
        li.attr("id",i);
        li.attr("limit",limit)
        $(".pagination").append(li);
    }

    $("#pageSize").change(function () {
        var id = 1;
        limit = $("#pageSize").val();
        var data2 = {"id":id,"limit":limit};
        page(data2)
    })

}


$("#commitStu1").click(function () {
      var stu = $("#addStu").serialize();
    $.ajax({
        url: "addStu",
        data: stu,
        dataType: "text",
        type: "post",
        success: function (data) {
            alert(data);
           window.location.href = "studentInit"
        }
    })
})



function update(data) {
    var id = data.id;

    $.ajax({
        url:"findAllStuById",
        data:{"id":id},
        type:"post",
        success:function (data) {
                $("#id").val(id);
                $("#stu-name").val(data.sname);
                $("#stu-sex").val(data.ssex);
                $("#stu-phone").val(data.tel);
                $("#stu-hour").val(data.hour);
                $("#cname").val(data.cname);
        }
    })
}


$("#commitStu").click(function () {
    var stu = $("#updateStu").serialize();
    console.log(stu)
    $.ajax({
        url: "updateStu",
        data: stu,
        dataType: "text",
        type: "post",
        success: function (data) {
            alert(data);
            window.location.href = "studentInit"
        }
    })
})



$(document).ready(function(){
    $('#generate-excel').click(function(){
        setTimeout(function(){   // 增加延时的目的就是等页面刷新后再导出标签的数据
            $('#my_table').table2excel({
                exclude: ".noExl",
                filename : "学生表格" + ".xls"
            })
        }, 500)
    })
})

$(function() {
    $("#my_table").xlsTableFilter();
});





